package com.five.taotao.social;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.five.taotao.R;
import com.five.taotao.entity.Product;
import com.five.taotao.entity.Store;

import java.util.List;

/**
 * Created by xiaohongmao on 2016/11/7.
 */
public class SocialAdapter  extends BaseAdapter{
    private List<Store> mData;
    private Context mContext;
    public SocialAdapter(List<Store> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         Store store = mData.get(position);
        //布局加载器
        LayoutInflater inflater = LayoutInflater.from(mContext);//参数要有 上下文 Context
         ViewHolder holder;
        /**
         * ConvertView 回收视图，效率提高 200%
         *
         * 如果原先没有convertView，就加载布局
         */
        if (convertView == null) {
            //加载布局
            convertView = inflater.inflate(R.layout.social_item, null, false);
            //缓存对象
            holder = new ViewHolder();//
            holder.img1 = (ImageView) convertView.findViewById(R.id.image1);
            holder.img2= (ImageView) convertView.findViewById(R.id.image2);
            holder.tv1= (TextView) convertView.findViewById(R.id.tv1);
            holder.tv2= (TextView) convertView.findViewById(R.id.tv2);
            holder.tv3= (TextView) convertView.findViewById(R.id.tv3);
            holder.btn= (Button) convertView.findViewById(R.id.btn);

             convertView.setTag(holder);//View 关联 Holder
        } else {
        //若果有 加载 ViewHolder
            holder= (ViewHolder) convertView.getTag();
        }
        holder.img1.setImageResource(store.getImg1());
        holder.img2.setImageResource(store.getImg2());
         holder.tv1.setText(store.getTv1());
         holder.tv2.setText(store.getTv2());
         holder.tv3.setText(store.getTv3());
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"关注成功",Toast.LENGTH_LONG);
            }
        });
        return convertView;//返回View对象
    }
    static class ViewHolder {
        ImageView img1;
        ImageView img2;
        TextView tv1;
        TextView tv2;
        TextView tv3;
        Button btn;
    }
}
